// 调整预览窗格的大小
export function addEditorEvent (cb:object) {
  const editorDom = document.querySelector('.editor') as HTMLDivElement
  let viewerDom = document.querySelector('.viewer') as HTMLDivElement
  let contentSplitDom = document.querySelector('.editor-split') as HTMLDivElement
  let isPress = false

  function editorMouseDown (e:MouseEvent) {
    isPress = true
    e.preventDefault()
    e.stopPropagation()

    let timer:number = 0
    let start = e.clientX

    function movefn (e:MouseEvent) {
      if (isPress) {
        e.preventDefault()
        e.stopPropagation()
        clearTimeout(timer)
        timer = setTimeout(function (e:MouseEvent) {
          const diff = e.clientX - start
          start = e.clientX
          let editorWidth = editorDom.offsetWidth
          let viewerWidth = viewerDom.offsetWidth

          if (viewerWidth <= 200 && diff > 0) {
            return
          }

          editorDom.style.width = editorWidth + diff + 'px'
          viewerDom.style.width = viewerWidth - diff + 'px'

          if (typeof cb === 'function') {
            cb()
          }
        }, 16, e)
      }
    }

    function upfn (e:MouseEvent) {
      if (isPress) {
        clearTimeout(timer)
        e.stopPropagation()
        e.preventDefault()
        isPress = false
        document.removeEventListener('mousemove', movefn)
        document.removeEventListener('mouseup', upfn)
      }
    }

    document.addEventListener('mousemove', movefn)
    document.addEventListener('mouseup', upfn)
  }

  contentSplitDom.addEventListener('mousedown', editorMouseDown)
  window.addEventListener('resize', function (e) {
    let listDom = document.querySelector('.note-list') as HTMLDivElement
    editorDom.style.width = '30%'
    viewerDom.style.width = `calc(100% - ${listDom.offsetWidth + 'px'} - 10px - 30%)`
  })
}

// 调整笔记列表的大小
export function addNoteListEvent (cb:object) {
  const editorDom = document.querySelector('.editor') as HTMLDivElement
  let listDom = document.querySelector('.note-list') as HTMLDivElement
  let noteSplitDom = document.querySelector('.note-split') as HTMLDivElement
  let isPress = false

  function noteListMouseDown (e:MouseEvent) {
    isPress = true
    e.preventDefault()
    e.stopPropagation()

    let timer:number = 0
    let start = e.clientX

    function movefn (e:MouseEvent) {
      if (isPress) {
        e.preventDefault()
        e.stopPropagation()
        clearTimeout(timer)
        timer = setTimeout(function (e:MouseEvent) {
          const diff = e.clientX - start
          let editorWidth = editorDom.offsetWidth
          let viewerWidth = listDom.offsetWidth
          editorDom.style.width = editorWidth - diff + 'px'
          listDom.style.width = viewerWidth + diff + 'px'
          start = e.clientX
        }, 16, e)
      }
    }

    function upfn (e:MouseEvent) {
      if (isPress) {
        clearTimeout(timer)
        e.stopPropagation()
        e.preventDefault()
        isPress = false
        document.removeEventListener('mousemove', movefn)
        document.removeEventListener('mouseup', upfn)
      }
    }

    document.addEventListener('mousemove', movefn)
    document.addEventListener('mouseup', upfn)
  }

  noteSplitDom.addEventListener('mousedown', noteListMouseDown)
}
